package org.ccunix.eshop.controller.api;
/**
 * 商品类别管理控制器 增 删 改 查
 */

import com.alibaba.fastjson.JSON;
import org.ccunix.eshop.domain.dto.CategoryVO;
import org.ccunix.eshop.service.CategoryServiceIface;
import org.ccunix.eshop.service.impl.CategoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/api/category")
public class CategoryController extends HttpServlet {
    CategoryServiceIface categoryService = new CategoryServiceImpl();
    String method = "";
    @Override
    //get请求
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        method = request.getParameter("m");//m 自定义的key
        if ("list".equals(method)){
            list(request,response);
        }else if ("del".equals(method)){
            delete(request,response);
        }
    }
    @Override
    //添加数据 Post
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if ("add".equals(method)){
            add(request,response);
        }
        if ("update".equals(method)){
            update(request,response);
        }
    }
    //method长的一模一样的方法
    //添加商品数据
    private void add(HttpServletRequest request, HttpServletResponse response) {
    }
    //更新商品数据
    private void update(HttpServletRequest request, HttpServletResponse response) {
    }
    //删除商品类型数据
    private void delete(HttpServletRequest request, HttpServletResponse response) {
    }
    //查询商品列表数据
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        List<CategoryVO> categoryList = new ArrayList<>();
//        CategoryVO vo = new CategoryVO();
//        vo.setId(1);
//        vo.setCateName("计算机类");
//        vo.setCateDesc("计算机类型的书籍");
//        CategoryVO vo2 = new CategoryVO();
//        vo2.setId(2);
//        vo2.setCateName("英语类");
//        vo2.setCateDesc("英语类类型的书籍");
//        CategoryVO vo3 = new CategoryVO();
//        vo3.setId(3);
//        vo3.setCateName("历史类");
//        vo3.setCateDesc("历史类类型的书籍");
//
//        categoryList.add(vo);
//        categoryList.add(vo2);
//        categoryList.add(vo3);
        List<CategoryVO> categoryList = categoryService.getCategoryList();//获得列表数据
        //把对象转换成json字符串
        String data = JSON.toJSONString(categoryList);
        //写回到前端  调用回复数据
        response.getWriter().write(data);
    }
}
